package com.michaelbulava.TFS.ui.commands;//package com.michaelbulava.TFS.ui.commands;
//
//import com.intellij.openapi.diagnostic.Logger;
//import com.intellij.openapi.progress.ProgressIndicator;
//import com.michaelbulava.TFS.Messages;
//import com.michaelbulava.TFS.log.FileLogger;
//import com.michaelbulava.TFS.ui.config.ClientConnectionAdvisor;
//import com.microsoft.tfs.core.TFSConfigurationServer;
//import com.microsoft.tfs.core.TFSConnection;
//import com.microsoft.tfs.core.TFSTeamProjectCollection;
//import com.microsoft.tfs.core.config.ConnectionAdvisor;
//import com.microsoft.tfs.core.exceptions.TFSAccessException;
//import com.microsoft.tfs.core.exceptions.TFSFederatedAuthException;
//import com.microsoft.tfs.core.exceptions.TFSUnauthorizedException;
//import com.microsoft.tfs.core.httpclient.Credentials;
//import com.microsoft.tfs.core.httpclient.UsernamePasswordCredentials;
//import com.microsoft.tfs.core.util.URIUtils;
//import com.microsoft.tfs.core.ws.runtime.exceptions.TransportRequestHandlerCanceledException;
//import com.microsoft.tfs.util.Check;
//
//import java.net.URI;
//import java.text.MessageFormat;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * Created by mbulava on 5/6/2015.
// */
//public class ConnectToConfigurationServerCommand implements ConnectCommand {
//    private static final Logger log = FileLogger.getInstance(ConnectToConfigurationServerCommand.class);
//    private URI serverURI;
//    private Credentials credentials;
//    private TFSConnection connection;
//
//    @Override
//    public TFSConnection getConnection() {
//        return null;
//    }
//
//    @Override
//    public void run(ProgressIndicator indicator) throws Exception {
//        ConnectionAdvisor connectionAdvisor = new ClientConnectionAdvisor();
//
//        String username = getUsername();
//        String message;
//        if (username != null){
//            String messageFormat = Messages.getString("ConnectToConfigurationServerCommand.ConnectingToServerAsFormat");
//            message = MessageFormat.format(messageFormat, new Object[]{this.serverURI.toString(), username});
//        } else {
//            String messageFormat = Messages.getString("ConnectToConfigurationServerCommand.ConnectionToServerFormat");
//            message = MessageFormat.format(messageFormat, new Object[] { this.serverURI.toString() });
//        }
//        log.info(message);
//        indicator.setText2(message);
//
//        List<ConnectionURIAndType> connectionTypes = new ArrayList();
//
//        connectionTypes.add(new ConnectionURIAndType(TFSConfigurationServer.class, this.serverURI));
//        if ("/tfs".equals(this.serverURI.getPath())) {
//            connectionTypes.add(new ConnectionURIAndType(TFSConfigurationServer.class, URIUtils.resolve(this.serverURI, "/")));
//        }
//        connectionTypes.add(new ConnectionURIAndType(TFSTeamProjectCollection.class, this.serverURI));
//        if ("/tfs".equals(this.serverURI.getPath())) {
//            connectionTypes.add(new ConnectionURIAndType(TFSTeamProjectCollection.class, URIUtils.resolve(this.serverURI, "/")));
//        }
//        this.connection = null;
//        while(connectionTypes.size() > 0){
//            ConnectionURIAndType connectionData = (ConnectionURIAndType)connectionTypes.remove(0);
//            try {
//                if (TFSConfigurationServer.class.equals(connectionData.getType())) {
//                    this.connection = new TFSConfigurationServer(connectionData.getURI(), this.credentials, connectionAdvisor);
//                } else if (TFSTeamProjectCollection.class.equals(connectionData.getType())) {
//                    this.connection = new TFSTeamProjectCollection(connectionData.getURI(), this.credentials, connectionAdvisor);
//                } else {
//                    throw new RuntimeException("Unknown connection type");
//                }
//                this.connection.authenticate();
//            } catch (Exception e) {
//                if (this.connection != null)
//                {
//                    this.credentials = this.connection.getCredentials();
//
//                    this.connection.close();
//                    this.connection = null;
//                }
//                if (((e instanceof TFSUnauthorizedException)) || ((e instanceof TFSAccessException)) || ((e instanceof TFSFederatedAuthException))) {
//                    throw e;
//                }
//                if ((e instanceof TransportRequestHandlerCanceledException)) {
//                    indicator.cancel();
//                }
//                if (connectionTypes.size() == 0) {
//                    throw e;
//                }
//                String messageFormat = "Server not found at {0}";
//
//                message = MessageFormat.format("Server not found at {0}", new Object[] { e.getLocalizedMessage() });
//                log.info(message);
//            }
//        }
//        Check.notNull(this.connection, "connection");
//        if (this.connection instanceof TFSTeamProjectCollection &&
//                ((TFSTeamProjectCollection)this.connection).getConfigurationServer() != null)
//            this.connection = ((TFSTeamProjectCollection)this.connection).getConfigurationServer();
//    }
//
//    @Override
//    public boolean isCancellable() {
//        return true;
//    }
//
//    @Override
//    public String getName() {
//        //TODO add to resources.
//        return "Connect to Configuration Server";
//    }
//
//
//    private String getUsername()
//    {
//        if ((this.credentials != null) && ((this.credentials instanceof UsernamePasswordCredentials))) {
//            return ((UsernamePasswordCredentials)this.credentials).getUsername();
//        }
//        return null;
//    }
//
//    private static final class ConnectionURIAndType
//    {
//        private final Class<? extends TFSConnection> type;
//        private final URI uri;
//
//        public ConnectionURIAndType(Class<? extends TFSConnection> type, URI uri)
//        {
//            Check.notNull(type, "type");
//            Check.notNull(uri, "uri");
//
//            this.type = type;
//            this.uri = uri;
//        }
//
//        public Class<? extends TFSConnection> getType()
//        {
//            return this.type;
//        }
//
//        public URI getURI()
//        {
//            return this.uri;
//        }
//    }
//}
